package com.david.mydemo.util;

import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.springframework.core.env.StandardEnvironment;

/**
 * 加密工具
 */
public class JasyptUtil {

    public static final String ALGORITHM = "PBEWithMD5AndDES";
    private static String PASSWORD_KEY = "jasypt.encryptor.password";
    private static String PASSWORD = "hxtt029";

    public static String encrypt(String str) {
        System.setProperty(PASSWORD_KEY, PASSWORD);
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());
        String result = stringEncryptor.encrypt(str);
//        System.out.println("加密后： " + result);
        return result;
    }

    public static String encrypt(String str, String password) {
        System.setProperty(PASSWORD_KEY, password);
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());
        String result = stringEncryptor.encrypt(str);
//        System.out.println("加密后： " + result);
        return result;
    }

    public static String encryptPBE(String text, String password) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(ALGORITHM);
        config.setPassword(password);
        encryptor.setConfig(config);
        return encryptor.encrypt(text);
    }

    public static String decrypt(String str) {
        System.setProperty(PASSWORD_KEY, PASSWORD);
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());
        String result = stringEncryptor.decrypt(str);
//        System.out.println("解密后： " + result);
        return result;
    }

    public static String decrypt(String str, String password) {
        System.setProperty(PASSWORD_KEY, password);
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());
        String result = stringEncryptor.decrypt(str);
//        System.out.println("解密后： " + result);
        return result;
    }

    public static String decryptPBE(String text, String password) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(ALGORITHM);
        config.setPassword(password);
        encryptor.setConfig(config);
        return encryptor.decrypt(text);
    }

    public static void main(String[] args) {
        String password = "QWER236269F8B0502EE8CFE663D7UIOP"; // MD5
        String str = "sde";
        String encrypt = JasyptUtil.encrypt(str, password);
        System.out.println("加密后： " + encrypt);
        System.out.println("解密后： " + JasyptUtil.decrypt(encrypt, password));
        System.out.println("解密后： " + JasyptUtil.decrypt("pNfWW0iGJGJD0mvhzH7YmA==", password));
    }

}
